<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $dailyReportMasterId     = $_REQUEST['dailyReportMasterId'];
  $currentCreateDate       = date("d-m-Y");
  $userName                = array();
  $createDate              = "";
  $userId                  = array();
  $partyId                 = "";
  $outCome                 = "";
  $remarks                 = "";
  $nextFollowUp            = "";
  $typeOfConclusionId      = "";
  $typeOfConclusion        = "";
  $companyName             = "";
  $marketFeedbackSelected  = "";
  $compInformationSelected = "";
  $dailyReportDetailArray  = array();
  $dailyReportCount        = 0; 
  $userSelected            = 0; 
  $rownum        = 1;
  $reportDateSelected      = "";
  if(isset($_POST['cancelBtn']))
  {
    header("Location:dailyReportList.php");
    exit();
  }
  if(isset($_POST['partyId']))
  { 
    $marketFeedback  = isset($_POST['marketFeedback']) ? $_POST['marketFeedback'] : " ";
    $compInformation = isset($_POST['compInformation']) ? $_POST['compInformation'] : " ";
    
    $updateDailyReport = "UPDATE dailyreportmaster 
                             SET marketFeedback = '".$marketFeedback."',
                                 compInformation = '".$compInformation."'
                           WHERE dailyReportMasterId =".$dailyReportMasterId;
    $updateDailyReportResult = mysql_query($updateDailyReport);
    $loopCount = 0;
    while($loopCount < count($_POST['outCome']))
    {
      $partyId         = ($_POST['partyId'][$loopCount] != '') ? $_POST['partyId'][$loopCount] : 0;
      $outCome         = ($_POST['outCome'][$loopCount] != '') ? $_POST['outCome'][$loopCount] : 0;
      $remarks         = isset($_POST['remarks'][$loopCount]) ? $_POST['remarks'][$loopCount] : "";
      $nextFollowUp    = isset($_POST['nextFollowUp'][$loopCount]) ? 
                               $_POST['nextFollowUp'][$loopCount*3+2]['Year']."-".$_POST['nextFollowUp'][$loopCount*3+1]['Month']."-".$_POST['nextFollowUp'][$loopCount*3]['Day']: '0000-00-00';
      $typeOfConclusionId  = ($_POST['typeOfConclusionId'][$loopCount] != '') ? $_POST['typeOfConclusionId'][$loopCount] : 0;
      if($_POST['partyId'][$loopCount] != "" || $_POST['outCome'][$loopCount] != "" || $_POST['typeOfConclusionId'][$loopCount] != "" )
      {
        $insertDeliveryChallanDetail = "INSERT INTO dailyreportdetail (dailyReportMasterId,partyId,
                                                                       outCome,remarks,nextFollowUp,typeOfConclusionId)
                                             VALUES (".$dailyReportMasterId.",'".$partyId."',
                                                     '".$outCome."','".$remarks."',
                                                     '".$nextFollowUp."','".$typeOfConclusionId."')";
        $insertDeliveryChallanDetailResult = mysql_query($insertDeliveryChallanDetail);
        if(!$insertDeliveryChallanDetailResult)
          die("Insert Query Not Inserted : ".mysql_error());
        else
          header("Location:dailyReportList.php");
      } 
      $loopCount++;
    } 
  }
  
  if(isset($_REQUEST['dailyReportMasterId']) && $_REQUEST['dailyReportMasterId'] > 0)
  {                                                                              
    $selectdailyReportEntry = "SELECT dailyreportmaster.dailyReportMasterId,dailyreportmaster.marketFeedback,dailyreportmaster.compInformation,user.userId,user.userName,dailyreportmaster.reportDate,
                                      dailyreportdetail.dailyReportMasterId,dailyreportdetail.dailyReportDetaileId,
                                      party.partyId,party.companyName,dailyreportdetail.outCome,dailyreportdetail.remarks,
                                      DATE_FORMAT(dailyreportdetail.nextFollowUp,'%d-%m-%Y') AS nextFollowUp,typeofconclusion.typeOfConclusionId,typeofconclusion.typeOfConclusion
				  						           FROM dailyreportmaster
				  						           JOIN dailyreportdetail
					 						           JOIN user   
						 					           JOIN party
											           JOIN typeofconclusion 
											          WHERE user.userId   = dailyreportmaster.userId
											            AND dailyreportmaster.dailyReportMasterId    = dailyreportdetail.dailyReportMasterId
											            AND party.partyId  = dailyreportdetail.partyId 
											            AND typeofconclusion.typeOfConclusionId = dailyreportdetail.typeOfConclusionId
											            AND dailyreportdetail.dailyReportMasterId = ".$_REQUEST['dailyReportMasterId'];  
    $selectdailyReportEntryResult = mysql_query($selectdailyReportEntry);
    $dailyReportRow = mysql_num_rows($selectdailyReportEntryResult);
    if($dailyReportRow > 0)
    {
	    while($dailyReportRow = mysql_fetch_array($selectdailyReportEntryResult))
	    {
	      $dailyReportDetailArray[$dailyReportCount]['rownum']  = $rownum;
	      $userSelected                                                      = $dailyReportRow['userId'];
		    $reportDateSelected                                                = $dailyReportRow['reportDate'];
		    $marketFeedbackSelected                                            = $dailyReportRow['marketFeedback'];
		    $compInformationSelected                                           = $dailyReportRow['compInformation'];
		    $dailyReportDetailArray[$dailyReportCount]['dailyReportMasterId']  = $dailyReportRow['dailyReportMasterId'];
		    $dailyReportDetailArray[$dailyReportCount]['dailyReportDetaileId'] = $dailyReportRow['dailyReportDetaileId'];
		    $dailyReportDetailArray[$dailyReportCount]['partyId']              = $dailyReportRow['partyId'];
		    $dailyReportDetailArray[$dailyReportCount]['companyName']          = $dailyReportRow['companyName'];
		    $dailyReportDetailArray[$dailyReportCount]['outCome']              = $dailyReportRow['outCome'];
		    $dailyReportDetailArray[$dailyReportCount]['typeOfConclusionId']   = $dailyReportRow['typeOfConclusionId'];
		    $dailyReportDetailArray[$dailyReportCount]['typeOfConclusion']     = $dailyReportRow['typeOfConclusion'];
		    $dailyReportDetailArray[$dailyReportCount]['remarks']              = $dailyReportRow['remarks'];
		    $dailyReportDetailArray[$dailyReportCount]['nextFollowUp']         = $dailyReportRow['nextFollowUp'];
		    $rownum ++;
	      $dailyReportCount++; 
	    } 
    }
  }
  ///// Select user : Start
  $selectUser = "SELECT userId,userName
                   FROM user";
  $selectUserResult = mysql_query($selectUser);
  $a = 0;
  while($userRow = mysql_fetch_array($selectUserResult))
  { 
    $userId[$a]   = $userRow['userId'];
    $userName[$a] = $userRow['userName'];
    $a++;
  }
  
  ///// Select party : Start
  $selectParty = "SELECT partyId,companyName
                   FROM party";
  $selectPartyResult = mysql_query($selectParty);
  $b = 0;
  while($partyRow = mysql_fetch_array($selectPartyResult))
  { 
    $partyId[$b]     = $partyRow['partyId'];
    $companyName[$b] = $partyRow['companyName'];
    $b++;               
  }
  
   ///// Select typeOfConclusion : Start                                 
  $selectConclusions = "SELECT typeOfConclusionId,typeOfConclusion                 
                          FROM typeofconclusion";                              
  $selectConclusionsResult = mysql_query($selectConclusions);            
  $c = 0;                                                    
  while($conclusionsRow = mysql_fetch_array($selectConclusionsResult))   
  {                                                          
    $typeOfConclusionId[$c]  = $conclusionsRow['typeOfConclusionId'];                 
    $typeOfConclusion[$c]    = $conclusionsRow['typeOfConclusion'];             
    $c++;                                                    
  }           
  
  include("./bottom.php");
  $smarty->assign("currentCreateDate",$currentCreateDate);
  $smarty->assign("userId",$userId);
  $smarty->assign("userName",$userName);
  $smarty->assign("createDate",$createDate);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("outCome",$outCome);
  $smarty->assign("remarks",$remarks);
  $smarty->assign("nextFollowUp",$nextFollowUp);
  $smarty->assign("typeOfConclusion",$typeOfConclusion);
  $smarty->assign("userSelected",$userSelected);
  $smarty->assign("reportDateSelected",$reportDateSelected);
  $smarty->assign("marketFeedbackSelected",$marketFeedbackSelected);
  $smarty->assign("compInformationSelected",$compInformationSelected);
  $smarty->assign("rownum",$rownum);
  $smarty->assign("dailyReportDetailArray",$dailyReportDetailArray);
  $smarty->assign("typeOfConclusionId",$typeOfConclusionId);
  $smarty->assign("dailyReportMasterId",$dailyReportMasterId);
  $smarty->display("dailyReportEdit.tpl");
}
?>